<?php
/**
 * @title 2462. 雇佣 K 位工人的总代价
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $costs
     * @param Integer $k
     * @param Integer $candidates
     * @return Integer
     */
    function totalCost($costs, $k, $candidates) {
        /**
         * @since 2024-01-06 元素数量
         */
        $count = count($costs);

        /**
         * @since 2024-01-06 总代价
         */
        $totalCost = 0;

        /**
         * @since 2024-01-06 前candidates个，后candidates个，覆盖costs所有元素
         */
        if($candidates*2 >= $count){
            /**
             * @since 2024-01-06 取costs前k个最小值
             * @since 2024-01-06 先进k个元素入堆
             */
            $maxHeap = new SplMaxHeap();
            for ($i=0; $i<$k; $i++){
                $totalCost += $costs[$i];
                $maxHeap->insert($costs[$i]);
            }

            /**
             * @since 2024-01-06 遍历剩下的元素，更小值入堆
             */
            for(; $i<$count; $i++){
                $top = $maxHeap->top();
                if($costs[$i] < $top){
                    $maxHeap->extract();
                    $maxHeap->insert($costs[$i]);
                    $totalCost = $totalCost - $top + $costs[$i];
                } else {}
            }

            /**
             * @return Integer
             */
            return $totalCost;
        } else {}

        /**
         * @since 2024-01-06 前candidates个左边最小堆，后candidates个右边最小堆
         * @since 2024-01-06 中间元素双指针[left, right]
         */
        $left = $candidates;
        $right = $count - $candidates - 1;

        /**
         * @since 2024-01-06 左边最小堆
         */
        $leftHeap = new SplMinHeap();
        for ($i=0; $i<$left; $i++){
            $leftHeap->insert($costs[$i]);
        }

        /**
         * @since 2024-01-06 右边最小堆
         */
        $rightHeap = new SplMinHeap();
        for ($i=$right+1; $i<$count; $i++){
            $rightHeap->insert($costs[$i]);
        }

        /**
         * @since 2024-01-06 遍历k次
         */
        for($i=0; $i<$k; $i++){
            /**
             * @since 2024-01-06 左边最小堆最小值，右边最小堆最小值
             */
            if($leftHeap->isEmpty()){
                $leftTop = PHP_INT_MAX;
            } else {
                $leftTop = $leftHeap->top();
            }
            if($rightHeap->isEmpty()){
                $rightTop = PHP_INT_MAX;
            } else {
                $rightTop = $rightHeap->top();
            }

            /**
             * @since 2024-01-06 左边元素出堆
             */
            if($leftTop <= $rightTop){
                $totalCost += $leftTop;
                $leftHeap->extract();

                /**
                 * @since 2024-01-06 left指针元素入堆
                 */
                if($left <= $right){
                    $leftHeap->insert($costs[$left]);
                    $left++;
                } else {}
            } else {
                /**
                 * @since 2024-01-06 右边元素出堆
                 */
                $totalCost += $rightTop;
                $rightHeap->extract();

                /**
                 * @since 2024-01-06 right指针元素入堆
                 */
                if($left <= $right){
                    $rightHeap->insert($costs[$right]);
                    $right--;
                } else {}
            }
        }

        /**
         * @return Integer
         */
        return $totalCost;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.06_2.php
 */
$datas = [
    [[17,12,10,2,7,2,11,20,8], 3, 4], // 11
    [[1,2,4,1], 3, 3], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->totalCost(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
